package cn.djb.day1;

import java.util.Stack;

/**
 * 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class CQueue {

    private Stack<Integer> left;

    private Stack<Integer> right;


    public CQueue() {
        left = new Stack<>();
        right = new Stack<>();

    }

    public void appendTail(int value) {
            left.push(value);

    }

    public int deleteHead() {

        if (0 == left.size()) {
            return -1;
        } else if (1 == left.size()) {
            return left.pop();
        } else {
            //移入 right
            while (!left.isEmpty()) {
                right.push(left.pop());
            }
            Integer pop = right.pop();
            //倒回去
            while (!right.isEmpty()) {
                left.push(right.pop());
            }
            return pop;
        }


    }

    //["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
    public static void main(String[] args) {
        CQueue cQueue = new CQueue();
        System.out.println(cQueue.deleteHead());
        cQueue.appendTail(5);
        cQueue.appendTail(2);
//        System.out.println(cQueue.deleteHead());
        System.out.println(cQueue.deleteHead());
        cQueue.appendTail(5);
        cQueue.appendTail(2);
        System.out.println(cQueue.deleteHead());
        System.out.println(cQueue.deleteHead());

    }
}
